/*
 * @Author: wurangkun
 * @Date: 2024-05-28 20:17:14
 * @LastEditTime: 2024-07-24 11:17:33
 * @LastEditors: wurangkun
 * @FilePath: \my-app\app\(tabs)\index.tsx
 * @Description: 
 */
import { router } from "expo-router";
import { useEffect } from "react";
import { Button, DeviceEventEmitter, Text, View } from "react-native";
import { userUserStore } from "../store/user";
import { to } from "@/hooks/routerHooks";
import Page from "@/components/page";
import Animated from "react-native-reanimated";
// import { DrawerActions, useNavigation } from "@react-navigation/native";

export default function Index() {
  // const navigation = useNavigation()
  const userInfo = userUserStore((state) => state.userInfo)
  useEffect(() => {

    // 组件已挂载，可以在这里执行需要的操作  
    console.log('组件已挂载:userInfo', userInfo);
    const sub = DeviceEventEmitter.addListener('refresh_tab', () => {
      console.log('refresh_tab')
    })

    if (!userInfo) {
      console.log(userInfo)
      setTimeout(() => {
        router.replace("/pages/login")
      }, 0)
      // return
    }
    // 清理函数（可选）  
    return () => {
      // 组件卸载前执行的清理工作  
      console.log('组件即将卸载');
      sub.remove()
    };
  }, [])

  const toHaha = () => {
    // to("/pages/haha");
    to("/pages/haha")
  }

  const toHiehei = () => {
    to("/pages/heiehi");
  }

  const toProfile = () => {
    to("/pages/userInfo/profile");
  }

  const toList = () => {
    to("/pages/list");
  }

  // const openDrawer = () => {
  //   navigation.dispatch(DrawerActions.openDrawer());
  // }

  return (
    <Page title="首页" showLeft={false}>
      <View
        style={{
          flex: 1,
          justifyContent: "center",
          alignItems: "center",
        }}
      >
        <View style={{
          width: 750,
          height: 100,
          backgroundColor: "red",
        }}></View>
        <Text>Edit app/index.tsx to edit this screen.</Text>
        <Animated.View style={{ width: 100, height: 100, backgroundColor: 'red' }} sharedTransitionTag='view' />
        <Button title="haha" onPress={toHaha}></Button>
        <Button title="toHiehei" onPress={toHiehei}></Button>
        <Button title="toProfile" onPress={toProfile}></Button>
        <Button title="toList" onPress={toList}></Button>
      </View>
    </Page>
  );
}
